/*
 * Copyright 2007 Agile Plugins Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * 	http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.googlecode.agileplugins.cruisewatcher;

import javax.swing.table.AbstractTableModel;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;


public class CruiseWatcherTableModel extends AbstractTableModel {
    private static final String[] NAMES = {"Name", "RSS Url", "Result URL", "Broken Image Dir", "Fixed Image Dir"};
    List rows = new ArrayList();
    private boolean dirty = false;

    public CruiseWatcherTableModel(List currentList) {
        setBuilds(currentList);
    }

    public int getRowCount() {
        return rows.size() + 1;
    }

    public int getColumnCount() {
        return 5;
    }

    public String getColumnName(int i) {
        return NAMES[i];
    }

    public Class getColumnClass(int col) {
        return String.class;
    }

    public Object getValueAt(int row, int col) {
        if (row == rows.size()) {
            return "";
        }
        CruiseBuild cruiseTable = (CruiseBuild) this.rows.get(row);
        switch (col) {
            case 0:
                return cruiseTable.getName();
            case 1:
                return cruiseTable.getRssUrl();
            case 2:
                return cruiseTable.getResultUrl();
            case 3:
                return cruiseTable.getBrokenFolder();
            case 4:
                return cruiseTable.getFixedFolder();
            default:
                return "***";
        }
    }

    public boolean isCellEditable(int row, int col) {
        return true;
    }

    public void setValueAt(Object value, int row, int col) {
        String text = (String) value;
        if (row == rows.size()) {
            rows.add(row, new CruiseBuild("", "", "", "", ""));
//            addRow(row);
        }
        CruiseBuild cruiseTable = (CruiseBuild) this.rows.get(row);
        switch (col) {
            case 0:
                cruiseTable.setName(text);
                break;
            case 1:
                cruiseTable.setRssUrl(text);
                break;
            case 2:
                cruiseTable.setResultUrl(text);
                break;
            case 3:
                cruiseTable.setBrokenFolder(text);
                break;
            case 4:
                cruiseTable.setFixedFolder(text);
                break;
        }
        fireTableCellUpdated(row, col);
        dirty = true;
    }

    public void addRow(int row) {
        if (row >= 0 && row < rows.size()) {
            rows.add(row, new CruiseBuild("", "", "", "", ""));
            fireTableRowsInserted(row, row);
        }
    }

    public Iterator iterator() {
        return rows.iterator();
    }

    public void removeRow(int selectedRow) {
        if (selectedRow >= 0 && selectedRow < rows.size()) {
            rows.remove(selectedRow);
            fireTableRowsDeleted(selectedRow, selectedRow);
        }
    }

    public boolean isDirty() {
        return dirty;
    }

    public void resetDirty() {
        dirty = false;
    }

    public List getBuilds() {
        return rows;
    }

    public void setBuilds(List newToggles) {
        rows.clear();
        rows.addAll(newToggles);
        fireTableDataChanged();
        resetDirty();
    }
}
